<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '发货' : '发货'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-table v-if="subOrder.length !== 0"
      :data="subOrder"
      border
      style="width: 100%">
      <el-table-column
        prop="name"
        label="商品图">
        <template slot-scope="scope">
        <el-popover
          placement="right"
          trigger="hover">
          <img :src="'http://image.yilianbaihui.cn/consumer/'+scope.row.path+'/'+scope.row.name"/>
          <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/'+scope.row.path+'/'+scope.row.name" style="max-height: 30px;max-width: 80px">
        </el-popover>
      </template>
      </el-table-column>
      <el-table-column
        prop="goodsName"
        label="商品名">
        <template slot-scope="scope">
          <a :href="'http://www.yilianbaihui.cn/item_show.html?goodsId=' + scope.row.goodsId">{{scope.row.goodsName}}</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="companyId"
        label="物流公司">
        <template slot-scope="scope">
          <el-select v-model="subOrder[scope.$index].shipCodeId" placeholder="物流公司">
            <el-option
              v-for="item in dataForm.list"
              :key="item.id"
              :label="item.companyName"
              :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="shipCode"
        label="物流单号">
        <template slot-scope="scope">
          <el-input v-model="subOrder[scope.$index].shipCodeNum" type="text"/>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-form-item label="订单号" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单号" disabled></el-input>
    </el-form-item>
    <el-form-item label="物流公司" prop="companyId">
      <el-select v-model="dataForm.companyId" placeholder="物流公司">
          <el-option
            v-for="item in dataForm.list"
            :key="item.id"
            :label="item.companyName"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="物流单号" prop="shipCode">
      <el-input v-model="dataForm.shipCode" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        subOrder: [],
        dataForm: {
          id: 0,
          orderId: '',
          shipCode: [],
          list: [],
          companyId: [],
          userId: ''
        },
        dataRule: {
          addtime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, orderId, userId) {
        this.dataForm.list = []
        this.dataForm.orderId = orderId
        this.dataForm.id = id || 0
        this.dataForm.userId = userId
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/notIssuedGoods/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.subOrder = data.data
              }
            })
          }
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/queryLogisticsCompany`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.list = data.data
                // this.dataForm.companyName = data.data.companyName
                // this.dataForm.companyId = data.data.companyId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            for (var i = 0; i < this.subOrder.length; i++) {
              if (this.subOrder[i].shipCodeId === null || this.subOrder[i].shipCodeNum === null) {
                this.$message.error('还有未发货的订单发填写物流信息')
                return false
              }
            }
            let cartId = ''
            let shipId = ''
            let shipCode = ''
            for (var j = 0; j < this.subOrder.length; j++) {
              cartId = cartId + ',' + this.subOrder[j].cartId
              shipId = shipId + ',' + this.subOrder[j].shipCodeId
              shipCode = shipCode + ',' + this.subOrder[j].shipCodeNum
            }
            this.$http({
              url: this.$http.adornUrl(`/shopping/order/modifyOrderShipmentById/${this.dataForm.id}/${shipId}/${shipCode}/${cartId}/${this.dataForm.userId}/${this.dataForm.orderId}`),
              method: 'post',
              data: this.$http.adornData()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
            // this.$http({
            //   url: this.$http.adornUrl(`/shopping/order/modifyOrderShipmentById`),
            //   method: 'post',
            //   data: this.$http.adornData({
            //     'id': this.dataForm.id || undefined,
            //     'companyId': this.dataForm.companyId,
            //     'shipCode': this.dataForm.shipCode
            //   })
            // }).then(({data}) => {
            //   if (data && data.code === 0) {
            //     this.$message({
            //       message: '操作成功',
            //       type: 'success',
            //       duration: 1500,
            //       onClose: () => {
            //         this.visible = false
            //         this.$emit('refreshDataList')
            //       }
            //     })
            //   } else {
            //     this.$message.error(data.msg)
            //   }
            // })
          }
        })
      }
    }
  }
</script>
